import {Dialog, Toast} from "vant";


export default {
    install: function (Vue, options) {

        /**
         * 返回上一页
         * -1,-2,-3
         *
         */
        Vue.prototype.Back = function () {
            this.$router.back(-1);
        };


        /**
         * BRToast 弹出
         * @param msg
         * @constructor
         */
        Vue.prototype.BRToast = function (msg = "提示") {
            Toast({
                mask: false, //是否显示蒙版
                message: msg,
            });
        };

        /**
         * BRDialog 弹出
         * @param message
         * @param callback
         * @param title
         * @constructor
         */
        Vue.prototype.BRDialog = function (message, callback, title = "提示") {
            Dialog.confirm({
                title: title,
                message: message,
                showCancelButton: false, //取消按钮
            })
                .then(() => {
                    if (typeof callback === "function") {
                        callback();
                    }
                })
                .catch(() => {
                    if (typeof callback === "function") {
                        callback();
                    }
                });
        };

        /**
         *
         * @param title
         * @param duration
         * @constructor
         */
        Vue.prototype.BRShowLoad = function (title = "加载中", duration = 2000) {
            Toast.loading({
                mask: true,
                message: title,
                duration: duration, //当前显示时间
                forbidClick: true, //禁止点击背景
            });
        };

        /**
         *
         * @constructor
         */
        Vue.prototype.BRHideLoad = function () {
            Toast.clear(true);
        };


        Vue.prototype.BRIsSetString = function (str) {
            if (str !== null && str !== undefined && str !== "") {
                return true;
            } else {
                return false;
            }
        };

        /**
         * 手机号码校验
         * @param phone
         * @returns {*}
         */
        Vue.prototype.BRIsPhone = function (phone) {
            if (!this.BRIsSetString(phone)) {
                Toast("请输入手机号");
                return 0;
            }
            let patterns = /^1[2,3,4,5,6,7,8,9]\d{9}$/;
            if (!patterns.test(phone)) {
                Toast("请输入正确手机号");
                return 0;
            }
            return phone;
        };

        /**
         * 4位验证码校验
         * @param Code
         * @returns {*}
         */
        Vue.prototype.BRIsCode = function (Code) {
            if (!this.BRIsSetString(Code)) {
                Toast("请输入验证码");
                return 0;
            }
            let reg = /^[0-9A-Za-z]{4}$/;
            if (!reg.test(Code)) {
                Toast("手机验证码错误");
                return 0;
            } else {
                return Code;
            }
        };

        /**
         * 4位验证码校验
         * @param identify
         * @returns {*}
         */
        Vue.prototype.BRIsIdentify = function (identify) {
            let reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
            if (reg.test(identify) === false) {
                return false;
            }
            return true;
        };
    },
};
